#!/usr/bin/env python
# coding: utf-8

# In[15]:


import os
import numpy as np
import pandas as pd
from jm.library.data_helper import date_to_str
from jm.library.likelihood import Likelihood


# In[16]:


df_status = pd.read_csv('data/jesus_maria_status_deidentified.csv')


# In[17]:

os.chdir('estimation/simulation_estimates/')
basesim = pd.read_csv('base_config_estimates.csv', index_col=0)
base_sim_control = pd.read_csv('base_config_control_estimates.csv', index_col=0)
basesim_fewer_bins = pd.read_csv('base_config4bins_estimates.csv', index_col=0)
basesim_more_bins = pd.read_csv('base_config20bins_estimates.csv', index_col=0)

controlsim = pd.read_csv('control_config_control_total_due_estimates.csv', index_col=0)
controlsim_fewer_bins = pd.read_csv('control_config_control_total_due4bins_estimates.csv', index_col=0)
controlsim_more_bins = pd.read_csv('control_config_control_total_due20bins_estimates.csv', index_col=0)
control_total_due_set_all_to_mean = pd.read_csv('control_config_control_total_due_total_due_set_all_to_mean_estimates.csv', index_col=0)
control_total_due_less_than_3000 = pd.read_csv('control_config_control_total_due_total_due_less_than_3000_estimates.csv', index_col=0)

implementation_2022_matching_rec1_total_due_rank = pd.read_csv('implementation_2022_matching_rec1_total_due_estimates.csv', index_col=0)
implementation_2022_matching_rec1_control_total_due_rank = pd.read_csv('implementation_2022_matching_rec1_control_total_due_estimates.csv', index_col=0)

longer_G1_more_G1_actual_estimates = pd.read_csv('longer_G1_more_G1_actual_estimates.csv', index_col=0)
longer_G1_more_G1_actual_control_estimates = pd.read_csv('longer_G1_more_G1_actual_control_estimates.csv', index_col=0)

longer_G1_more_G1_conservative = pd.read_csv('longer_G1_more_G1_conservative_estimates.csv', index_col=0)
longer_G1_more_G1_conservative_control = pd.read_csv('longer_G1_more_G1_conservative_control_estimates.csv', index_col=0)
longer_G1_more_G1_more_rec1 = pd.read_csv('longer_G1_more_G1_more_rec1_estimates.csv', index_col=0)



longer_G1_more_G1_matching_rec1 = pd.read_csv('longer_G1_more_G1_matching_rec1_estimates.csv', index_col=0)
control_longer_G1_more_G1_matching_rec1 = pd.read_csv('longer_G1_more_G1_matching_rec1_control_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_rank = pd.read_csv(
    'longer_G1_more_G1_matching_rec1_total_due_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_total_due_rank = pd.read_csv(
    'longer_G1_more_G1_matching_rec1_control_total_due_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_score_exo_covariates = pd.read_csv('longer_G1_more_G1_matching_rec1_score_exo_covariates_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_score_exo_covariates = pd.read_csv('longer_G1_more_G1_matching_rec1_control_score_exo_covariates_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_score_endo_covariates = pd.read_csv('longer_G1_more_G1_matching_rec1_score_endo_covariates_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_score_endo_covariates = pd.read_csv('longer_G1_more_G1_matching_rec1_control_score_endo_covariates_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_random_order = pd.read_csv('longer_G1_more_G1_matching_rec1_random_order_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_random_order = pd.read_csv('longer_G1_more_G1_matching_rec1_control_random_order_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_activatingG2 = pd.read_csv('longer_G1_more_G1_matching_rec1_total_due_activatingG2_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_shutdownG3 = pd.read_csv('longer_G1_more_G1_matching_rec1_total_due_shutdownG3_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3 = pd.read_csv('longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean = pd.read_csv('longer_G1_more_G1_matching_rec1_total_due_total_due_set_all_to_mean_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_total_due_less_than_3000 = pd.read_csv('longer_G1_more_G1_matching_rec1_total_due_total_due_less_than_3000_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_total_due_set_all_to_mean = pd.read_csv('longer_G1_more_G1_matching_rec1_control_total_due_total_due_set_all_to_mean_estimates.csv', index_col=0)
longer_G1_more_G1_matching_rec1_control_total_due_less_than_3000 = pd.read_csv('longer_G1_more_G1_matching_rec1_control_total_due_total_due_less_than_3000_estimates.csv', index_col=0)


os.chdir('../../')

# In[18]:


def mean_se(sim_num, sim_denom=controlsim['estimates'], file=None):
    treatment_effect = sim_num/sim_denom
    upper = np.quantile(treatment_effect, 0.975)
    lower = np.quantile(treatment_effect, 0.025)
    print('Expected effect:' + str(np.mean(treatment_effect)), file=file)
    print('95% confidence interval: [' + str(np.round(lower-1,4)) + ", " + str(np.round(upper-1,4)) + "]", file=file)


def get_cols_over_time(col_fmt='payments_by_{}'):
    return [col_fmt.format(date_to_str(d)) for d in Likelihood.event_dates]


# In[19]:


payment_cols = get_cols_over_time()
priority_cols = get_cols_over_time('priority_by_{}')
action_cols = get_cols_over_time('action_by_{}')
action_nomedida_cols = get_cols_over_time('action_no_medida_by_{}')
relative_payment_cols = ['relative_' + c for c in payment_cols]
fwdiff_payment_cols = ['fwdiff_' + p for p in payment_cols]
relative_fwdiff_payment_cols = ['relative_' + c for c in fwdiff_payment_cols]


df_status[relative_payment_cols] = df_status[payment_cols].divide(
                df_status['total_due'], axis=0)


df_status[relative_fwdiff_payment_cols] = df_status[fwdiff_payment_cols].divide(
    df_status['total_due'], axis=0)


with open("figs/table8_tableOC4.txt", "w") as text_file:
    print('CF1: ', file=text_file)
    print(" ", file=text_file)
    print("VS Control:", file=text_file)
    mean_se(longer_G1_more_G1_actual_estimates['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control:", file=text_file)
    mean_se(longer_G1_more_G1_actual_estimates['estimates'],
            sim_denom=longer_G1_more_G1_actual_control_estimates['estimates'], file=text_file)
    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_actual_estimates['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_actual_estimates['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_actual_estimates['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('CF2:', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(basesim['estimates'], sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(basesim['estimates'], sim_denom=base_sim_control['estimates'], file=text_file)
    print(" ")
    print("average # medidas:" + str(np.mean(basesim['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(basesim['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(basesim['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('CF3:', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_conservative['estimates'], sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_conservative['estimates'], sim_denom=longer_G1_more_G1_conservative_control['estimates'], file=text_file)
    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_conservative['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_conservative['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_conservative['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('CF4:', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1['estimates'], sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1['estimates'], sim_denom=control_longer_G1_more_G1_matching_rec1['estimates'], file=text_file)
    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # notifications:" + str(np.mean(longer_G1_more_G1_matching_rec1['valor_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('CF5:', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_rank['estimates'], sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_rank['estimates'], sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_rank['estimates'], file=text_file)
    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_rank['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_rank['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_rank['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('CF6:', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(implementation_2022_matching_rec1_total_due_rank['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(implementation_2022_matching_rec1_total_due_rank['estimates'],
            sim_denom=implementation_2022_matching_rec1_control_total_due_rank['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(implementation_2022_matching_rec1_total_due_rank['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(implementation_2022_matching_rec1_total_due_rank['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(implementation_2022_matching_rec1_total_due_rank['G1_priority_by_2021-09-06'])), file=text_file)


with open("figs/tableOC2.txt", "w") as text_file:
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_more_rec1['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_more_rec1['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_more_rec1['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_more_rec1['G1_priority_by_2021-09-06'])), file=text_file)


with open("figs/tableOC1.txt", "w") as text_file:
    print('row 1 of table OC1 excluded, since it replicates a row from table 8', file=text_file)
    print('', file=text_file)
    print('score exo covariates', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_score_exo_covariates['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_score_exo_covariates['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_score_exo_covariates['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_exo_covariates['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_exo_covariates['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_exo_covariates['G1_priority_by_2021-09-06'])), file=text_file)

    print(" ", file=text_file)
    print('score endo covariates', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_score_endo_covariates['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_score_endo_covariates['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_score_endo_covariates['estimates'], file=text_file)

    print(" ")
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_endo_covariates['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_endo_covariates['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_score_endo_covariates['G1_priority_by_2021-09-06'])), file=text_file)

    print('random score', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_random_order['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_random_order['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_random_order['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_random_order['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_random_order['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_random_order['G1_priority_by_2021-09-06'])), file=text_file)


with open("figs/tableOC3.txt", "w") as text_file:
    print('activating G2', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_activatingG2['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_activatingG2['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_rank['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2['G1_priority_by_2021-09-06'])), file=text_file)

    print('shutdown G3', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_shutdownG3['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_shutdownG3['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_rank['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_shutdownG3['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_shutdownG3['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_shutdownG3['G1_priority_by_2021-09-06'])), file=text_file)

    print('activate G2, shutdown G3', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3['estimates'],
            sim_denom=controlsim['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_rank['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_activatingG2_shutdownG3['G1_priority_by_2021-09-06'])), file=text_file)


with open("figs/tableOC5.txt", "w") as text_file:
    print('total due equal to the mean', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean['estimates'],
            sim_denom=control_total_due_set_all_to_mean['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_set_all_to_mean['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_set_all_to_mean['G1_priority_by_2021-09-06'])), file=text_file)

    print('total due <=3000', file=text_file)
    print(" ", file=text_file)
    print("VS Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_less_than_3000['estimates'],
            sim_denom=control_total_due_less_than_3000['estimates'], file=text_file)
    print(" ", file=text_file)
    print("VS Precise Control", file=text_file)
    mean_se(longer_G1_more_G1_matching_rec1_total_due_less_than_3000['estimates'],
            sim_denom=longer_G1_more_G1_matching_rec1_control_total_due_less_than_3000['estimates'], file=text_file)

    print(" ", file=text_file)
    print("average # medidas:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_less_than_3000['medida_action_by_2021-09-06'])), file=text_file)
    print("average # rec1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_less_than_3000['rec1_action_by_2021-09-06'])), file=text_file)
    print("average # G1:" + str(np.mean(longer_G1_more_G1_matching_rec1_total_due_less_than_3000['G1_priority_by_2021-09-06'])), file=text_file)

with open('numbers/secOB4.txt', 'w') as text_file:
    print('fewer bins', file=text_file)
    mean_se(basesim_fewer_bins['estimates'],
            sim_denom=controlsim_fewer_bins['estimates'], file=text_file)

    print('', file=text_file)

    print('more bins', file=text_file)
    mean_se(basesim_more_bins['estimates'],
           sim_denom=controlsim_more_bins['estimates'], file=text_file)
